package com.link.play.fit.ai.ui

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.animation.AccelerateInterpolator
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.AnticipateOvershootInterpolator
import android.view.animation.ScaleAnimation
import android.view.animation.TranslateAnimation
import androidx.activity.addCallback
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.link.play.fit.ai.ad.AdmobOpen
import com.link.play.fit.ai.ad.AdsShowCallBack
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.base.goToAc
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.databinding.AcSplashBinding
import com.link.play.fit.ai.kv.AuthCache
import com.link.play.fit.ai.kv.AuthCache.isGuideHello
import com.link.play.fit.ai.kv.AuthCache.isInitGuide
import com.link.play.fit.ai.kv.AuthCache.userAgreementAgree
import com.link.play.fit.ai.store.CloudStore
import com.link.play.fit.ai.ui.dialog.UserAgreementDialog
import com.link.play.fit.ai.ui.guide.ac.GuideHelloAc
import com.link.play.fit.ai.ui.guide.ac.GuideLottieAc
import com.link.play.fit.ai.ui.guide.ac.GuidePbAc
import com.link.play.fit.ai.ui.guide.ac.WelcomeAc
import com.link.play.fit.ai.ui.main.MainAc
import kotlinx.coroutines.Job
import com.link.play.fit.ai.utils.EventUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.util.concurrent.atomic.AtomicBoolean

/**
 *Create by SleepDog on 2025-03-24
 * 闪屏页
 */
class SplashAc : BaseActivity<AcSplashBinding>(AcSplashBinding::inflate) {

    private var job: Job? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        onBackPressedDispatcher.addCallback { }

        lifecycleScope.launch {
            EventUtils.event("app_start")
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                //android12以后有默认闪屏，等一下它结束动画效果
                delay(200)
            }
            val scaleAnimation = ScaleAnimation(
                1f, 2500f,
                1f, 2500f,
                Animation.RELATIVE_TO_SELF,
                0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f
            )
            scaleAnimation.duration = 2500
            scaleAnimation.fillAfter = true
            binding.root.setBackgroundColor(Color.WHITE)
            binding.ivCircle.startAnimation(scaleAnimation)

            val animationSet = AnimationSet(false)
            val scaleAnimation1 = ScaleAnimation(
                1f, 2f,
                1f, 2f,
                Animation.RELATIVE_TO_SELF,
                0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f
            )
            scaleAnimation1.duration = 400
            scaleAnimation1.fillAfter = true
            scaleAnimation1.interpolator = AnticipateOvershootInterpolator()
            scaleAnimation1.startOffset = 500

            val scaleAnimation2 = ScaleAnimation(
                1f, 0f,
                1f, 0f,
                Animation.RELATIVE_TO_SELF,
                0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f
            )
            scaleAnimation2.duration = 300
            scaleAnimation2.fillAfter = true
            scaleAnimation2.interpolator = AccelerateInterpolator()
            scaleAnimation2.startOffset =
                scaleAnimation1.duration + scaleAnimation1.startOffset + 600

            val alphaAnimation1 = AlphaAnimation(0f, 1f)
            alphaAnimation1.duration = scaleAnimation1.duration
            alphaAnimation1.fillAfter = true
            alphaAnimation1.startOffset = scaleAnimation1.startOffset

            val alphaAnimation2 = AlphaAnimation(1f, 0f)
            alphaAnimation2.duration = scaleAnimation2.duration
            alphaAnimation2.fillAfter = true
            alphaAnimation2.startOffset = scaleAnimation2.startOffset

            animationSet.addAnimation(scaleAnimation1)
            animationSet.addAnimation(scaleAnimation2)
            animationSet.addAnimation(alphaAnimation1)
            animationSet.addAnimation(alphaAnimation2)
            animationSet.fillAfter = true
            binding.ivLogo.isVisible = true
            binding.ivLogo.startAnimation(animationSet)
            delay(alphaAnimation2.startOffset + alphaAnimation2.duration + 100)
            val translateAnimation = TranslateAnimation(
                Animation.RELATIVE_TO_SELF,
                0f,
                Animation.RELATIVE_TO_SELF,
                1f,
                Animation.RELATIVE_TO_SELF,
                0f,
                Animation.RELATIVE_TO_SELF,
                0f,
            )
            translateAnimation.duration = 180
            translateAnimation.fillAfter = true
            binding.vTitle.isVisible = true
            binding.tvTitle.isVisible = true
            binding.tvTitle.text = binding.tvTitle.text.toString().replace(" ", "")
            binding.vTitle.startAnimation(translateAnimation)
            delay(translateAnimation.duration + 800)


            val jumpAction = {
                if (!AuthCache.isVip) {
//                job = launch {
//                    delay(3000)
//                    if (isActive) {
//                        jumpNext()
//                    }
//                }
//                AdmobOpen.show(this@SplashAc, object : AdsShowCallBack() {
//                    override fun show() {
//                        job?.cancel()
//                    }
//
//                    override fun close(where: Int) {
//                        jumpNext()
//                    }
//
//                    override fun failed(where: Int) {
//                        job?.cancel()
//                        jumpNext()
//                    }
//                })
                    jumpNext()
                } else {
                    jumpNext()
                }
            }

            if (!userAgreementAgree) {
                val dialog = UserAgreementDialog(this@SplashAc)
                dialog.yAction = {
                    userAgreementAgree = true
                    jumpNext()
                }
                dialog.nAction = {
                    finish()
                }
                dialog.showDialog()
            } else {
                jumpNext()
            }

        }
    }

    private val jumped = AtomicBoolean(false)
    private fun jumpNext() {

        if (jumped.get()) return
        jumped.set(true)

        if (!isInitGuide) {//走引导流程
            if (!isGuideHello) {
//                goToAc(GuideHelloAc::class.java)
                goToAc(WelcomeAc::class.java)
            } else {
                goToAc(GuidePbAc::class.java)

            }
        } else {
            goToAc(MainAc::class.java)
        }

        finish()
    }

    override fun useDefaultImmersive() {
    }

}